package com.emode.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.xwork.StringUtils;

import com.emode.base.BaseDaoImpl;
import com.emode.entity.Bbsusers;
import com.emode.service.imp.IUserService;
@SuppressWarnings("unchecked")
public class UserService extends BaseDaoImpl<Bbsusers> implements IUserService{

	@Override
	public List findUsers(String string) {
		String hql = "from Bbsusers u where u.uname like %?%";
		return ht.find(hql,new Object[]{string});
	}	

	//更新登录时间
	@Override
	public void updateLoginDate(Bbsusers users) {
		//whereHql = "";
		String hql = "update Bbsusers u set u.lastlogin=? where u.uid = ?";
		updateObj(hql,new Object[]{users.getLastlogin(),users.getUid()});
	}
	

	/* (non-Javadoc)
	 * @see com.emode.service.imp.IUserService#check(com.emode.entity.Bbsusers)
	 */
	@Override
	public Bbsusers find(Bbsusers user) {
		String whereHql = "";
		List paramsList = new ArrayList();
		if(StringUtils.isNotBlank(user.getUname())){
			whereHql = whereHql+" and o.uname=?";
			paramsList.add(user.getUname());
		}
		if(StringUtils.isNotBlank(user.getUpassword())){
			whereHql = whereHql+" and o.upassword=?";
			paramsList.add(DigestUtils.md5Hex(user.getUpassword()));
		}
		if(StringUtils.isNotBlank(user.getUemail())){
			whereHql = whereHql+" and o.uemail=?";
			paramsList.add(user.getUemail());
		}
		Object[] values = paramsList.toArray();
		List<Bbsusers> list = findObjectByConditionWithNoPage(whereHql, values);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
		
	}



}
